package com.jdwx.opengl;

import android.opengl.GLES20;
import android.opengl.GLES30;
import android.util.Log;

import static android.opengl.GLES20.GL_COMPILE_STATUS;
import static android.opengl.GLES20.GL_FRAGMENT_SHADER;
import static android.opengl.GLES20.GL_LINK_STATUS;
import static android.opengl.GLES20.GL_TRUE;
import static android.opengl.GLES20.GL_VALIDATE_STATUS;
import static android.opengl.GLES20.GL_VERTEX_SHADER;
import static android.opengl.GLES20.glAttachShader;
import static android.opengl.GLES20.glCompileShader;
import static android.opengl.GLES20.glCreateProgram;
import static android.opengl.GLES20.glCreateShader;
import static android.opengl.GLES20.glDeleteProgram;
import static android.opengl.GLES20.glDeleteShader;
import static android.opengl.GLES20.glGetError;
import static android.opengl.GLES20.glGetProgramiv;
import static android.opengl.GLES20.glGetShaderiv;
import static android.opengl.GLES20.glLinkProgram;
import static android.opengl.GLES20.glShaderSource;
import static android.opengl.GLES20.glValidateProgram;

public class ShaderHelper {
    public static int compileVertexShader( String shaderCode ){
        return compileShader( GL_VERTEX_SHADER, shaderCode);
    }
    public static int compileFragmentShader( String shaderCode ){
        return compileShader( GL_FRAGMENT_SHADER, shaderCode);
    }
    //编译着色器
    public static int compileShader( int type, String shaderCode ){
        final int shaderObjectId = glCreateShader( type );
        if( shaderObjectId == 0 ){
            int a = glGetError();
            return 0;
        }

        glShaderSource( shaderObjectId, shaderCode );
        glCompileShader( shaderObjectId );
        final int[] compileStatus = new int[1];
        glGetShaderiv(shaderObjectId,GL_COMPILE_STATUS,compileStatus, 0 );
        if( compileStatus[0] == 0 ){
            //若编译失败则显示错误日志并删除此shader
            Log.e("ES30_ERROR", "Could not compile shader " + type + ":");
            Log.e("ES30_ERROR", GLES20.glGetShaderInfoLog(shaderObjectId));
            glDeleteShader( shaderObjectId );
            return 0 ;
        }
        return shaderObjectId;
    }
    //连接程序
    public static int linkProgram( int vertexShaderId, int fragmentShaderId){
        final int programObjectId = glCreateProgram();
        if(  programObjectId== 0 ){
            return 0;
        }
        glAttachShader( programObjectId, vertexShaderId );
        glAttachShader( programObjectId , fragmentShaderId );
        glLinkProgram( programObjectId );
        final int[] linkStatus = new int[1];
        glGetProgramiv(programObjectId,GL_LINK_STATUS, linkStatus, 0 );
        if( linkStatus[0] == 0 ){
            glDeleteProgram( programObjectId );
            //若编译失败则显示错误日志并删除此shader
            Log.e("ES30_ERROR", "Could not compile linkProgram linkProgram:");
            Log.e("ES30_ERROR", "==="+GLES20.glGetProgramInfoLog(programObjectId));
            return 0;
        }
        return programObjectId;
    }
    //验证
    public static boolean validataProgram( int programObjectId ){
        glValidateProgram(programObjectId  );
        final int[] validataStatus = new int[1];
        glGetProgramiv( programObjectId, GL_VALIDATE_STATUS, validataStatus, 0);
        return validataStatus[0] != 0;
    }


}
